<?php

namespace Common\Model;

use Think\Model;
use Think\Exception;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/21
 * Time: 13:57
 */
class BaseModel extends Model
{
    protected $mod = '';
    function __construct($table,$pre)
    {
        $this->mod = M($table, '', $pre);
    }

    /**
     * @param type $where 条件
     * @param type $page 分页
     * @param type $pageSize 每页几条记录
     * @param type $field 查询字段
     * @param type $order 排序
     * @return type
     */
    public function getList($where = '', $page=1, $pageSize = 15, $field = '*', $order='')
    {
        if (!empty($where)) {
            $this->mod->where($where);
        }
        if (empty($order)) {
            $order['id'] = 'desc';
        }
        if ($page > 0 && $pageSize > 0) {
            $res = $this->mod->field($field)->page($page)->limit($pageSize)->order($order)->select();
        } else {
            $res  = $this->mod->field($field)->order($order)->select();
        }
        return $res;
    }

    /**
     * @param type $where 更新条件
     * @param type $field 查询字段
     * @return type
     */
    public function getOne($where='', $field = '*', $order='', $limit = 1)
    {
        if (!empty($where)) {
            $this->mod->where($where);
        }
        if (!empty($order)) {
            $this->mod->order($order);
        }
        $res = $this->mod->field($field)->limit($limit)->find();
        return $res;
    }


    /**
     * @param type $data 要插入的字段
     * @return type
     */
    public function setAdd($data)
    {
        if ($this->create($data)) {
            $res = $this->mod->strict(true)->add();
            return $res;
        }
    }

    /**
     * @param $where
     * @param $param
     * @return bool
     */
    public function setSave($where, $param)
    {

        if ( !$where  || $where=='' || $where ==null){
            return false;
        }

        if ($param) {
            $ret = $this->mod->strict(true)->where($where)->save($param);
            return $ret;
        }

    }

}